package com.zidian.study.adapter;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import com.zidian.student.trucks.R;
import com.mingming.com.library.FlowTagLayout;
import com.mingming.com.library.OnTagSelectListener;

/**
 * Created by Administrator on 2016/8/24 0024.
 * 标签评价适配器
 */
public class EvaluateTagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List data;
    private List<String> textTag = new ArrayList<>();


    public EvaluateTagAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
        textTag.add("好评");
        textTag.add("好好评");
        textTag.add("差评");
        textTag.add("极差");
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent,
                false);
        return new ItemViewHolder(view);

    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).tagTextAdapter = new TagTextAdapter<>(context);
            ((ItemViewHolder) holder).tagEvaluate.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            ((ItemViewHolder) holder).tagEvaluate.setAdapter(((ItemViewHolder) holder).tagTextAdapter);
            ((ItemViewHolder) holder).tagTextAdapter.onlyAddAll(textTag);

            ((ItemViewHolder) holder).tagEvaluate.setOnTagSelectListener(new OnTagSelectListener() {
                @Override
                public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {

                    if (selectedList != null && selectedList.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        for (int i : selectedList) {
                            sb.append(parent.getAdapter().getItem(i));
                        }
                        Snackbar.make(parent, "选择的标签:" + sb.toString(), Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();
                    } else {
                        Snackbar.make(parent, "没有选择标签", Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();
                    }

                }
            });


        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {

        FlowTagLayout tagEvaluate;
        TagTextAdapter tagTextAdapter;


        public ItemViewHolder(View view) {
            super(view);
            tagEvaluate = (FlowTagLayout) view.findViewById(R.id.item_evaluate_tags1);
        }
    }

}
